package com.huali.dao;


import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class BaseDao01 {

    /**
     * 数据源对象
     */
    private static DataSource dataSource = null;

    /**
     * 初始化数据源 静态代码块
     */
    static {
        try {
            Properties properties = new Properties();
            properties.load(BaseDao01.class.getClassLoader().getResourceAsStream("druid.properties"));
            //使用德鲁伊数据源工厂创建数据源对象
            // ctrl + alt + l
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据库连接对象
     *
     * @return Connection
     * @throws SQLException
     */
    private static Connection getConnection() throws SQLException {

//        Connection connection = dataSource.getConnection();
//        return connection;

        return dataSource.getConnection();
    }

    /**
     * 数据库修改方法
     *
     * @param sql  sql语句
     * @param data 参数列表
     * @return int sql执行完影响的数据条数
     */
    public static int executeUpdate(String sql, Object... data) {
        //数据库连接对象
        Connection conn = null;
        //sql执行对象
        PreparedStatement ps = null;
        //返回结果
        int res = 0;
        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            //进行属性的赋值 属性赋值的下标从1开始
            for (int i = 0; i < data.length; i++) {
                ps.setObject(i + 1, data[i]);
            }
            res = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(conn,ps,null);
        }
        return res;
    }

    /**
     * 数据库查询方法
     * @param sql sql语句
     * @param data 参数列表
     * @return List<Map<String, Object>>
     */
    public static List<Map<String, Object>> executeQuery(String sql, Object... data) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < data.length; i++) {
                ps.setObject(i + 1, data[i]);
            }
            rs = ps.executeQuery();
            int count = rs.getMetaData().getColumnCount();
            while (rs.next()){
                Map<String, Object> map = new HashMap<>();
                for (int i = 1; i <= count; i++) {
                    String name = rs.getMetaData().getColumnLabel(i);
                    Object values = rs.getObject(i);
                    map.put(name, values);
                }
                list.add(map);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(conn, ps, rs);
        }
        return list;
    }




    /**
     * 释放资源
     *
     * @param conn 数据库连接对象
     * @param ps   sql执行对象
     * @param rs   返回数据结果集
     */
    private static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}
